
package kaizone.haining.chaobohui.json;

import java.util.HashMap;

import kaizone.haining.chaobohui.json.FirstReadDO.Data;

import org.json.JSONObject;

public class BannerDO extends JSONResponse {

    // // 海宁
    public static final String URL = "http://api.dp-app.com/chao/remote/rest/query_banner.htm?banners=index,small,big,bottom";

    // 移动
    // public static final String URL =
    // "http://static.dp-app.com/tags/cmcc.json";

    // 联通
    // public static final String URL = "http://static.dp-app.com/tags/cu.json";

    // 电信
    // public static final String URL = "http://static.dp-app.com/tags/ct.json";

    private HashMap<String, Banner> data;

    public HashMap<String, Banner> getData() {
        return data;
    }

    public void setData(HashMap<String, Banner> data) {
        this.data = data;
    }

    public Banner getBanner(String key) {
        if (data == null || data.isEmpty())
            return null;
        return data.get(key);
    }

    public static BannerDO parseJson(String str) {
        if (str != null && str.startsWith("\ufeff")) {
            str = str.substring(1);
        }

        BannerDO bannerDO = new BannerDO();
        try {
            JSONObject jsonObject = new JSONObject(str);
            JSONResponse response = JSONResponse.parse(jsonObject);
            bannerDO.setJSONResponse(response);

            JSONObject jsondata = jsonObject.optJSONObject(Data.DATA);
            if (jsondata != null) {
                HashMap<String, Banner> data = new HashMap<String, Banner>();
                JSONObject indexjson = jsondata.optJSONObject(Banner.INDEX);
                if (indexjson != null) {
                    Banner indexbanner = Banner.parse(indexjson);
                    data.put(Banner.INDEX, indexbanner);
                }

                JSONObject samlljson = jsondata.optJSONObject(Banner.SMALL);
                if (samlljson != null) {
                    Banner samllbanner = Banner.parse(samlljson);
                    data.put(Banner.SMALL, samllbanner);
                }

                JSONObject bigjson = jsondata.optJSONObject(Banner.BIG);
                if (bigjson != null) {
                    Banner bigbanner = Banner.parse(bigjson);
                    data.put(Banner.BIG, bigbanner);
                }

                JSONObject bottomjson = jsondata.optJSONObject(Banner.BOTTOM);
                if (bottomjson != null) {
                    Banner bottombanner = Banner.parse(bottomjson);
                    data.put(Banner.BOTTOM, bottombanner);
                }

                bannerDO.setData(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bannerDO;
    }

}
